<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="../layui/css/layui.css" type="text/css">
    <script src="../layui/layui.js" type="text/javascript"></script>
    <script type="text/javascript" src="../js/jquery-1.8.3.js"></script>
    <style>
        .layui-table-cell .layui-form-checkbox[lay-skin="primary"] {
            top: 50%;
            transform: translateY(-50%);
        }
        .layui-table-cell{
            text-align:center;
            height: auto!important;
            white-space: normal;
        }
    </style>
</head>
<body>

<div class="layui-container-fluid">
    <div class="layui-row">
        <div class="layui-col-xs12">
            <%--新增按钮--%>
            <button id="add_btn" class="layui-btn layui-btn-radius layui-btn-normal">新增</button>
            <div class="demoTable layui-input-inline">
                搜索：
                <div class="layui-inline">
                    <input class="layui-input" name="id" id="demoReload" autocomplete="off" placeholder="名字">
                </div>
                <button class="layui-btn" data-type="reload">搜索</button>
            </div>
            <%--table表格--%>
            <table id="demo" lay-filter="demo"></table>
        </div>
    </div>
</div>



<%--修改状态表单--%>
<form class="layui-form" style="display: none;padding: 10px 30px 0px 0px;" id="updateForm"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <input id="userId" type="hidden" name="userId">
    </div>
    <div class="layui-form-item" lay-filter="radio1">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block" id="d1">
            <input type="radio" name="userStatus" value="0" title="未激活" >
            <input type="radio" name="userStatus" value="1" title="正常" >
            <input type="radio" name="userStatus" value="2" title="冻结" >
            <input type="radio" name="userStatus" value="3" title="已删除" >
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateUserStatus">立即提交</button>
        </div>
    </div>
</form>

<script>
    //开启layui的各种组件
    layui.use(['layer','laydate','table','form','upload'],function () {
        var laydate=layui.laydate;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var upload=layui.upload;
        var path='${pageContext.request.contextPath}/';

        //一个修改弹出层，便于关闭弹出层
        var updateIndex;


        //渲染table表格
        table.render({
            elem: '#demo'//id选择器选择表格
            ,url: '${pageContext.request.contextPath}/cmfzUser/selectUserByPage' //数据接口
            ,page: true //开启分页
            ,limit:3
            ,limits:[3,5,10]
            ,id: 'demo'
            ,cols: [[ //表头
                {field: 'userId', title: 'ID',sort:true}
                ,{field: 'userTelphone',title: '电话'}
                ,{field: 'userPassword',title: '密码'}
                ,{field: 'userNickname',title: '昵称'}
                ,{field: 'userImage',title: '图片',templet: function(d){
                        return "<img src='"+path+d.userImage+"' height='80'/>";
                    }}
                ,{field: 'userName',title: '姓名'}
                ,{field: 'userSex',title: '性别'}
                ,{field: 'userAutograph',title: '个签'}
                ,{field: 'userProvince',title: '省份'}
                ,{field: 'userCity',title: '城市'}
                ,{field: 'userAutograph',title: '个签'}
                ,{field: 'guruId',title: '上师ID'}
                ,{field: 'userCreateDate',title: '创建时间'}
                ,{field: 'userStatus',title: '状态',templet:function(d){
                        if (d.userStatus==0) {
                            return '未激活';
                        }if (d.userStatus==1) {
                            return '正常';
                        }if (d.userStatus==2) {
                            return '冻结';
                        }if (d.userStatus==3) {
                            return '已删除';
                        }
                    }}
                ,{title:"操作", width:300,align:'center',toolbar: '#barDemo'}//引入外部的toolbar进行渲染
            ]]
        });

        /*
        搜索
        */
        var $ = layui.$, active = {
            reload: function(){
                var demoReload = $('#demoReload');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        key:demoReload.val()
                    }
                }, 'data');
            }
        };
        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });



        //监听表格的几个按钮
        table.on("tool(demo)",function (obj) {
            var data=obj.data;// 拿到该行的数据
            var layEvent=obj.event; //事件
            //修改
            if (layEvent=='edit'){
                //弹出层，并且给表单赋值
                updateIndex=layer.open({
                    type: 1,
                    content: $('#updateForm')
                });
                //第一个为lay-filter
                $("input[name=userStatus]").each(function () {
                    $(this).prop("checked",false);
                    if ($(this).val() == data.userStatus) {
                        $(this).prop("checked",true);
                    }
                });
                form.val("updateForm",data);
                $("#userId").val(data.userId);
                form.render();
            }

            if (layEvent=='del'){
                //删除操作
                layer.confirm('确定删除吗?', {icon: 3, title:'提示'}, function(index){
                    $.ajax({
                        url:"layui-user/deleteUser",
                        data:{id:data.id},
                        dataType:"json",
                        success:function (result) {
                            if (result.code==0){
                                //删除成功
                                layer.msg(result.msg);
                                //重新加载页面
                                table.reload('demo', {page: {
                                        curr: 1 //重新从第 1 页开始
                                    }});
                            } else {
                                layer.msg(result.msg);
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });

        //修改
        form.on("submit(updateUserStatus)",function (data) {
            $.ajax({
                url:"${pageContext.request.contextPath}/cmfzUser/updateUserStatus",
                type:"post",
                dataType:"json",
                data: data.field,
                success:function (result) {
                    if (result.flag==true){
                        //关闭弹出层，清空表单
                        layer.msg(result.message);
                        layer.close(updateIndex);
                        table.reload('demo');
                    } else {
                        layer.msg(result.message);
                    }
                }
            });
            return false;//防止表单跳转
        });
    })
</script>

<%--toolbar用于引入--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

</body>
</html>
